package com.ktwlrj.securitycenter.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.SafeInterceptRuleArgs;
import com.ktwlrj.securitycenter.dto.SafeInterceptRuleDto;
import com.ktwlrj.securitycenter.entity.SafeInterceptRuleEntity;
import com.ktwlrj.securitycenter.mapper.SafeInterceptRuleMapper;
import com.ktwlrj.securitycenter.service.SafeInterceptRuleService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 预警拦截规则业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:13
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeInterceptRuleServiceImpl extends ServiceImpl<SafeInterceptRuleMapper, SafeInterceptRuleEntity>
		implements SafeInterceptRuleService {
	private final SafeInterceptRuleMapper mapper;

	@Override
	public void batchSave(List<SafeInterceptRuleArgs> args) throws RuntimeException {
		// 删除预警拦截id下所有规则
		mapper.deleteByBusiId(args.get(0).getInterceptId());
		// 新增预警拦截规则
		List<SafeInterceptRuleEntity> entityList = ConvertUtil.listAtoListB(args, SafeInterceptRuleEntity.class);
		boolean result = super.saveBatch(entityList);
		if (!result) {
			throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
		}
	}

	@Override
	public List<SafeInterceptRuleDto> selectListByModel(String interceptId) throws RuntimeException {
		QueryWrapper<SafeInterceptRuleEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(SafeInterceptRuleEntity::getInterceptId, interceptId);
		List<SafeInterceptRuleEntity> entityList = mapper.selectList(queryWrapper);
		if (entityList == null || entityList.size() <= 0) {
			throw new ActionException(ResultCode.VERIFICATION_FAILED.getValue(), "未查询到预警拦截规则信息");
		}
		List<SafeInterceptRuleDto> interceptRuleDtoList = ConvertUtil.listAtoListB(entityList,
				SafeInterceptRuleDto.class);
		return interceptRuleDtoList;
	}
}